package com.gexiaobao.pigeon.ui.fragment.mine.organization.video

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.model.bean.GatherLiveResponse
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityGatherLiveBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterGatherLiveStation
import com.gexiaobao.pigeon.viewmodel.VideoViewModel
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/8/22 14:58
 * @Describe    : 直播
 */
class ActivityGatherLive : BaseActivity<VideoViewModel, ActivityGatherLiveBinding>() {

    private var mPassId = 0
    private var mUrlLive = ""
    private var mTitle = ""
    private val mAdapter: AdapterGatherLiveStation by lazy { AdapterGatherLiveStation(arrayListOf()) }

    private var isPulling = false

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mTitle = intent.getStringExtra("title").toString()
        mDatabind.includeBar.tvTitle.text = mTitle
        mPassId = intent.getIntExtra("passId", 0)
        onRefresh()

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(this), mAdapter)

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh()
        }

        mAdapter.setOnItemLiveClickListener(object : AdapterGatherLiveStation.OnItemLiveClickListener {
            override fun onItemClick(data: GatherLiveResponse.GatherLiveBean.ChannelListBean) {
                mUrlLive = data.liveAddress
                startLive(mUrlLive)
            }
        })
    }


    private fun startLive(mUrlLive: String) {
        isPulling = true
        if (mUrlLive.isEmpty()) {
            RxToast.warning(getString(R.string.The_live_address_is_empty))
            return
        }
        startActivity<ActivityLiveVideoPlay>("url" to mUrlLive)
    }

    private fun onRefresh() {
        if (mTitle == "集鸽直播" || mTitle == "集鸽回放") {
            mViewModel.getGatherLiveApp(mPassId)
        } else {
            mViewModel.getBackLiveApp(mPassId)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        /** 获取直播结果 */
        mViewModel.getGatherLiveAppResult.observe(this) {
            if (mDatabind.includeList.swipeRefreshLayout.isRefreshing) {
                mDatabind.includeList.swipeRefreshLayout.isRefreshing = false
            }
            mAdapter.setList(it.list)
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack, mDatabind.ivCloseLive) {
            when (it) {
                mDatabind.ivCloseLive -> {
                    isPulling = false
                    setViewState(false)
                }

                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }

    private fun setViewState(isPlaying: Boolean) {
        dismissLoading()
        if (isPlaying) {
            mDatabind.conLayout.isVisible = false
            mDatabind.ivCloseLive.isVisible = true
        } else {
            mDatabind.conLayout.isVisible = true
            mDatabind.ivCloseLive.isVisible = false
        }
    }
}